# coding: utf-8
import os
import sys
from sqlalchemy import Column, Integer, Text, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

Base = declarative_base()
metadata = Base.metadata


class EquipmentData(Base):
    __tablename__ = "equipment"
    id = Column(Integer, primary_key=True)
    iEid = Column(Text)
    """游戏内ID"""
    name = Column(Text)
    """装备名称"""
    lv = Column(Integer)
    """装备等级"""
    rarity = Column(Text)
    """装备稀有度"""
    icon = Column(Text)
    """装备图标"""
    typeSon1Id = Column(Text)
    """装备类别"""
    typeSon2Id = Column(Text)
    """部位"""
    typeSon3Id = Column(Text)
    """装备类型补充"""
    desc = Column(Text)
    """装备描述"""
    suit = Column(Text)
    """套装ID"""
    STR = Column(Float)
    """力量"""
    INT = Column(Float)
    """智力"""
    AtkP = Column(Float)
    """物理攻击"""
    AtkM = Column(Float)
    """魔法攻击"""
    CriticalMP = Column(Float)
    """魔法暴击率"""
    CriticalM = Column(Float)
    """魔法暴击"""
    CriticalPP = Column(Float)
    """物理暴击率"""
    CriticalP = Column(Float)
    """物理暴击"""
    SpeedA = Column(Float)
    """攻击速度"""
    SpeedM = Column(Float)
    """移动速度"""
    SpeedR = Column(Float)
    """施放速度"""
    HitP = Column(Float)
    """命中率"""
    Hit = Column(Float)
    """命中"""

    def toDict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class PropData(Base):
    __tablename__ = "prop"
    id = Column(Integer)
    iEid = Column(Text, primary_key=True)
    """游戏内ID"""
    name = Column(Text)
    """装备名称"""
    lv = Column(Integer)
    """装备等级"""
    rarity = Column(Text)
    """装备稀有度"""
    icon = Column(Text)
    """装备图标"""
    typeSon1Id = Column(Text)
    """装备类别"""
    typeSon2Id = Column(Text)
    """部位"""
    typeSon3Id = Column(Text)
    """装备类型补充"""
    desc = Column(Text)
    """装备描述"""

    def toDict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class SuitData(Base):
    __tablename__ = "suit"

    id = Column(Integer, primary_key=True)
    suitID = Column(Text)
    """套装ID"""
    suitName = Column(Text)
    """套装名称"""
    needNum = Column(Integer)
    """套装数量"""
    desc = Column(Text)
    """套装描述"""
    compatibility = Column(Text)
    """兼容于"""

    def toDict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class SealData(Base):
    __tablename__ = "seal"

    id = Column(Integer, primary_key=True)
    name = Column(Text)
    """名称"""
    lv = Column(Integer)
    """等级"""
    rarity = Column(Text)
    """稀有度"""
    typeSon1Id = Column(Text)
    """装备类别"""
    typeSon2Id = Column(Text)
    """部位"""
    typeSon3Id = Column(Text)
    """装备类型补充"""
    desc = Column(Text)
    """装备描述"""

    def toDict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


def get_engine(version="0"):
    try:
        os.chdir(os.path.dirname(sys.argv[0]))
    except Exception:
        pass
    return create_engine(
        "sqlite:///{}".format(
            os.path.join("./", "dataFiles", "mdcalc-{}.db".format(version))
        )
    )
